package com.yx.cloud.utils;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.yx.cloud.R;

/**
 * 通用确认对话框
 */
public class CommonConfirmDialog2 extends Dialog {

    private Context context;
    private String message;
    private ConfirmCallback confirmCallback;
    private String btn1Name,btn2Name;

    public CommonConfirmDialog2(Context context, String message, String btn1Name,String btn2Name,ConfirmCallback confirmCallback) {
        super(context, R.style.dialog);
        this.context = context;
        this.message = message;
        this.confirmCallback = confirmCallback;
        this.btn1Name = btn1Name;
        this.btn2Name = btn2Name;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_confirm2);

        TextView tv_message = findViewById(R.id.tv_confirm_text);
        TextView btn_confirm = findViewById(R.id.tv_dialog_confirm);
        TextView btn_cancel = findViewById(R.id.tv_dialog_cancel);
        if(btn1Name != null){
            btn_cancel.setText(btn1Name);
        }
        if(btn2Name != null){
            btn_confirm.setText(btn2Name);
        }
        tv_message.setText(message);
        setCanceledOnTouchOutside(false);

        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (RomUtils.isFastClick()) {
                    return;
                }
                if (confirmCallback != null) {
                    //如果confirm返回false, 不取消对话框
                    if (confirmCallback.onConfirm()) {
                        dismiss();
                    }
                } else {
                    dismiss();
                }
            }
        });
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (RomUtils.isFastClick()) {
                    return;
                }
                if (confirmCallback != null) {
                    if (confirmCallback.onCancel()) {
                        dismiss();
                    }
                }
                dismiss();
            }
        });
    }

    public interface ConfirmCallback {
        boolean onConfirm();

        boolean onCancel();
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    public static final class Builder {
        private Context context;
        private String message;
        private String btn1Name,btn2Name;
        private ConfirmCallback confirmCallback;

        private Builder() {
        }

        public Builder context(Context context) {
            this.context = context;
            return this;
        }

        public Builder message(String message) {
            this.message = message;
            return this;
        }
        public Builder btn2Name(String btn2Name) {
            this.btn2Name = btn2Name;
            return this;
        }
        public Builder btn1Name(String btn1Name) {
            this.btn1Name = btn1Name;
            return this;
        }

        public Builder confirmCallback(ConfirmCallback confirmCallback) {
            this.confirmCallback = confirmCallback;
            return this;
        }

        public CommonConfirmDialog2 build() {
            if (context == null) {
                throw new IllegalArgumentException("context should not be null");
            }
            CommonConfirmDialog2 commonConfirmDialog2 = new CommonConfirmDialog2(context, message,btn1Name,btn2Name, confirmCallback);
            return commonConfirmDialog2;
        }
    }

    @SuppressLint("RestrictedApi")
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {

        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            //do something.
            return true;
        } else {
            return super.dispatchKeyEvent(event);
        }
    }
}
